package com.buba.controller.jigou;

import com.buba.pojo.Area;
import com.buba.pojo.Dept;
import com.buba.pojo.User;
import com.buba.service.jigou.JigouService;
import com.buba.utils.MyContent;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import javax.servlet.http.HttpSession;
import javax.xml.ws.RequestWrapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("jigou")
@Controller
public class JigouController {
    Logger log= LogManager.getLogger();
    @Autowired
    @Qualifier("jigouService")
    JigouService jigouService;

    /**
     * 跳转至机构列表界面
     * @return
     */
    @RequestMapping("toListOrganization")
    public String toListOrganization(){
        return "jigou/organizationList";
    }

    /**
     * 机构列表
     * @return
     */
    @RequestMapping("listOrganization")
    @ResponseBody
    public Map<String,Object> listOrganization(){
        Map<String,Object> m=new HashMap<>(16);
        List<Area> userList=jigouService.listOrganization();
        if(userList.size()>0){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"查询成功");
            m.put(MyContent.DATA,userList);
        }else {
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"查询失败");
            m.put(MyContent.DATA,userList);
        }

        return m;
    }

    /**
     * 显示删除模态框的信息
     * @param id
     * @return
     */
    @RequestMapping("getAreaForDelete")
    @ResponseBody
    public Map<String,Object> getAreaForDelete(@RequestBody Integer id){
        Map<String,Object> m=new HashMap<>(16);
        Area area=jigouService.getAreaForDelete(id);
        if(area!=null){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"查询成功");
            m.put(MyContent.DATA_ONE,area);
        }else {
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"查询失败");
            m.put(MyContent.DATA_ONE,area);
        }
        return m;
    }
    /**
     * 根据id删除机构信息
     */
    @RequestMapping("deleteAreaById")
    @ResponseBody
    public Map<String,Object> deleteAreaById(@RequestBody Integer id){
        Map<String,Object> m=new HashMap<>(16);
        int i=jigouService.deleteAreaById(id);
        if(i>0){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"删除成功");
        }else {
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"删除失败");
        }
        return m;
    }

    /**
     * 跳转至添加中支页面
     * @return
     */
    @RequestMapping("toInsertArea")
    public String toInsertArea(){
        return "jigou/insertArea";
    }
    /**
     * 添加中支
     */
    @RequestMapping("insertArea")
    public String insertArea(Area area, HttpSession session,Model model){
        Map<String,Object> m=new HashMap<>(16);
        User user=(User)session.getAttribute(MyContent.DATA);
        area.setCreatedby(user.getId());
        area.setModifyby(user.getId());
        Area area1=jigouService.getAreaByAreaname(area.getAreaname());
        Area area2=jigouService.getAreaByAreacode(area.getAreacode());

        if(area1!=null||area2!=null){
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"添加失败，该机构已存在");
            model.addAttribute(MyContent.MSG,"添加失败，该机构已存在");
            return "jigou/insertArea";
        }else{
            int i=jigouService.insertArea(area);
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"添加成功");
            return "jigou/organizationList";
        }

    }
    /**
     * 跳转至修改页面
     */
    @RequestMapping("toUpdateArea")
    public String toUpdateArea(Integer id, Model model){
        model.addAttribute("id",id);
        return "jigou/updateArea";
    }


    /**
     * 查询中支信息，携带到修改页面
     * @param id
     * @return
     */
    @RequestMapping("getAreaForUpdate")
    @ResponseBody
    public Map<String,Object> getAreaForUpdate(@RequestBody Integer id){
        Map<String,Object> m=new HashMap<>(16);
        Area area=jigouService.getAreaForDelete(id);
        if(area!=null){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"查询成功");
            m.put(MyContent.DATA,area);
        }else{
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"查询失败");
        }
        return m;
    }

    /**
     * 修改中支机构
     * @param area
     * @return
     */
    @RequestMapping("updateArea")
    public String updateArea(Area area){
        Map<String,Object> m=new HashMap<>(16);
        int i=jigouService.updateArea(area);
        if(i>0){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"修改成功");
            return "/jigou/organizationList";
        }else{
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"修改失败");
            return "/jigou/updateArea";
        }
    }

    /**
     * 跳转至四级机构列表界面
     * @param parentid
     * @param model
     * @return
     */
    @RequestMapping("toDeptList")
    public String toDeptList(Integer parentid ,Model model){
        model.addAttribute("parentid",parentid);
        return "/jigou/deptList";
    }

    /**
     * 查询四级机构列表
     * @param parentid
     * @return
     */

    @RequestMapping("listDept")
    @ResponseBody
    public Map<String,Object> listDept(@RequestBody Integer parentid){
        Map<String,Object> m=new HashMap<>(16);
        List<Dept> deptList=jigouService.listDept(parentid);
        if(deptList.size()>0){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"查询成功");
            m.put(MyContent.DATA,deptList);
        }else {
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"查询失败");
            m.put(MyContent.DATA,deptList);
        }

        return m;
    }
    /**
     * 跳转至添加四级机构页面
     * @return
     */
    @RequestMapping("toInsertDept")
    public String toInsertDept(Integer parentid ,Model model){
        Area area= jigouService.getAreaForDelete(parentid);
        String areaname=area.getAreaname();
        model.addAttribute("parentid",parentid);
        model.addAttribute("areaname",areaname);
        return "jigou/insertDept";
    }

    /**
     * 添加中支
     * @param dept
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("insertDept")
    public String insertDept(Dept dept, HttpSession session,Model model){
        Map<String,Object> m=new HashMap<>(16);
        User user=(User)session.getAttribute(MyContent.DATA);
        dept.setCreatedby(user.getId());
        dept.setModifyby(user.getId());
        Dept dept1=jigouService.getDeptByDeptcode(dept.getDeptcode());
        Dept dept2=jigouService.getDeptByDeptname(dept.getDeptname());

        if(dept1!=null||dept2!=null){
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"添加失败，该机构已存在");
            model.addAttribute(MyContent.MSG,"添加失败，该机构已存在");
            return "jigou/insertDept";
        }else{
            int i=jigouService.insertDept(dept);
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"添加成功");
            model.addAttribute("parentid",dept.getParentid());
            return "jigou/deptList";
        }
    }

    /**
     * 显示删除模态框的信息(查询四级机构信息，携带到修改页面)
     * @param id
     * @return
     */
    @RequestMapping("getDeptInfo")
    @ResponseBody
    public Map<String,Object> getDeptInfo(@RequestBody Integer id){
        Map<String,Object> m=new HashMap<>(16);
        Dept dept=jigouService.getDeptInfo(id);
        if(dept!=null){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"查询成功");
            m.put(MyContent.DATA_ONE,dept);
        }else {
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"查询失败");
            m.put(MyContent.DATA_ONE,dept);
        }
        return m;
    }

    /**
     * 根据id删除机构信息
     */
    @RequestMapping("deleteDeptById")
    @ResponseBody
    public Map<String,Object> deleteDeptById(@RequestBody Integer id){
        Map<String,Object> m=new HashMap<>(16);
        int i=jigouService.deleteDeptById(id);
        if(i>0){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"删除成功");
        }else {
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"删除失败");
        }
        return m;
    }

    /**
     * 跳转至四级机构修改页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("toUpdateDept")
    public String toUpdateDept(Integer id, Model model){
        model.addAttribute("id",id);
        return "jigou/updateDept";
    }


    /**
     * 修改中支机构
     * @param dept
     * @return
     */
    @RequestMapping("updateDept")
    public String updateDept(Dept dept,Model model){
        Map<String,Object> m=new HashMap<>(16);
        int i=jigouService.updateDept(dept);
        if(i>0){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"修改成功");
            model.addAttribute("parentid",dept.getParentid());
            return "/jigou/deptList";
        }else{
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"修改失败");
            return "/jigou/updateDept";
        }
    }
}
